/*
 * @Author: 火红色祥云 809880192@qq.com
 * @Date: 2023-09-26 21:49:22
 * @LastEditors: 火红色祥云 809880192@qq.com
 * @LastEditTime: 2023-09-30 09:49:42
 * @FilePath: /imx6ull-app/24_modbus/rtu/main.cpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "modbus_rtu.hpp"
#include <unistd.h>
#if 1
#define DEV_UART    "/dev/ttymxc2"
#else
#define DEV_UART    "/dev/pts/2"
#endif



int main(int argc, char *argv[])
{
    uint8_t read_buffer[1024];
    uint8_t write_buffer[1024];
    uint16_t read_buffer_16[3];
    uint16_t write_buffer_16[1024];
    int8_t ret = 0;
    modbus::modbus_rtu modbus(DEV_UART, 115200, 'N', 8, 1);  //创建一个modbus_rtu对象

    for (int i = 0; i < 10; i++)
    {

        
        ret = modbus.read_coil_status(0x0000, 5, read_buffer);//00 01 00 00 00 06 01   01 67 89 00 05 
        if (ret > 0)
        {
            printf("read_coil_status:%d\r\n",ret);
            for (int j = 0; j < ret; j++)
            {
                printf("%02x ",read_buffer[j]);
            }
            printf("\r\n");
        }
        usleep(1000*100);

        ret = modbus.read_input_status(0x0000, 5, read_buffer);//00 01 00 00 00 06 01    01 67 89 00 05 
        
        if (ret > 0)
        {
            printf("read_input_status:%d\r\n",ret);
            for (int j = 0; j < ret; j++)
            {
                printf("%02x ",read_buffer[j]);
            }
            printf("\r\n");
        }
        usleep(1000*100);

        ret = modbus.read_holding_register(0x0000, 5, read_buffer_16);//00 01 00 00 00 06 01   01 67 89 00 05
        if (ret > 0)
        {
            printf("read_holding_register:%d\r\n",ret); 
            for (int j = 0; j < ret; j++)
            {
                printf("%02x ",read_buffer_16[j]);
            }
            printf("\r\n");
        }
        usleep(1000*100);

        ret = modbus.read_input_register(0x0000, 5, read_buffer_16);//00 01 00 00 00 06 01    04 67 89 00 05  
        if (ret > 0)
        {
            printf("read_input_register:%d\r\n",ret);
            for (int j = 0; j < ret; j++)
            {
                printf("%02x ",read_buffer_16[j]);
            }
            printf("\r\n");
        }
        usleep(1000*100);
        
        // printf("write_single_coil:%d\r\n",modbus.write_single_coil(0x0000, 0x1234));
        // usleep(1000*100);
        // printf("write_single_register:%d\r\n",modbus.write_single_register(0x0000, 0x1234));//0x06：写单个保持寄存器: 00 01 00 00 00 06 01    06 67 89 12 34
        // usleep(1000*100);
        // printf("write_multiple_coil:%d\r\n",modbus.write_multiple_coil(0x0000, 5, write_buffer));//0x0F（15）：写多个线圈: 00 01 00 00 00 08 01     0F 67 89 00 05 01 1F
        // usleep(1000*100);
        // printf("write_multiple_registers:%d\r\n",modbus.write_multiple_registers(0x0000, 5,  write_buffer_16));//0x10（16）：写多个保持寄存器: 00 01 00 00 00 11 01   10 67 89 00 05 0A 00 00 00 00 00 00 00 00 00 05 
        // usleep(1000*100);
    }
    usleep(1000*500);
}

